package com.dhu.stylefit.controller;

import com.dhu.stylefit.Response;
import com.dhu.stylefit.dto.ClothesDTO;
import com.dhu.stylefit.service.ClothesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
public class ClothesController {

    @Autowired
    private ClothesService clothesService;

    // 根据 ID 获取单个服装信息
    @GetMapping("/clothes/{id}")
    public Response<ClothesDTO> getClothesById(@PathVariable long id) {
        try {
            ClothesDTO clothesDTO = clothesService.getClothesById(id);
            return Response.newSuccess(clothesDTO);
        } catch (Exception e) {
            e.printStackTrace();
            return Response.newFail("Failed to get clothes by ID: " + e.getMessage());
        }
    }

    // 根据用户 ID 获取全部服装信息
    @GetMapping("/clothes/users/{userId}")
    public Response<List<ClothesDTO>> getClothesByUserId(@PathVariable long userId) {
        try {
            List<ClothesDTO> clothesDTOList = clothesService.getClothesByUserId(userId);
            return Response.newSuccess(clothesDTOList);
        } catch (Exception e) {
            e.printStackTrace();
            //return Response.newFail("Failed to get clothes by user ID: " + e.getMessage());
            return Response.newFail("未查询到任何衣物");
        }
    }

    // 添加一个服装信息
    @PostMapping("/clothes")
    public Response<Long> addNewClothes(@RequestBody ClothesDTO clothesDTO) {
        try {
            Long clothesId = clothesService.addNewClothes(clothesDTO);
            return Response.newSuccess(clothesId);
        } catch (Exception e) {
            e.printStackTrace();
            return Response.newFail("Failed to add new clothes: " + e.getMessage());
        }
    }

    // 根据 ID 删除服装信息
    @DeleteMapping("/clothes/{id}")
    public Response<String> deleteClothesById(@PathVariable long id) {
        try {
            clothesService.deleteClothesById(id);
            return Response.newSuccess("Clothes deleted successfully");
        } catch (Exception e) {
            return Response.newFail("Failed to delete clothes: " + e.getMessage());
        }
    }

    // 更新服装信息
    @PutMapping("/clothes/{id}")
    public Response<ClothesDTO> updateClothes(@PathVariable Long id, @RequestBody ClothesDTO clothesDTO) {
        try {
            clothesDTO.setClothesId(id);
            ClothesDTO updatedClothes = clothesService.updateClothes(clothesDTO);
            if (updatedClothes != null) {
                return Response.newSuccess(updatedClothes);
            }
            return Response.newFail("Clothes not found for update");
        } catch (Exception e) {
            e.printStackTrace();
            return Response.newFail("Failed to update clothes: " + e.getMessage());
        }
    }

    // 获取指定用户单个分类下全部服装
    @GetMapping("/clothes/users/{userId}/categories/{categoryId}")
    public Response<List<ClothesDTO>> getClothesByUserIdAndCategoryId(@PathVariable long userId, @PathVariable long categoryId) {
        try {
            List<ClothesDTO> clothesDTOList = clothesService.getClothesByUserIdAndCategoryId(userId, categoryId);
            return Response.newSuccess(clothesDTOList);
        } catch (Exception e) {
            e.printStackTrace();
            //return Response.newFail("Failed to get clothes by user ID and category ID: " + e.getMessage());
            return Response.newFail("未查询到任何衣物");
        }
    }

}
